package cn.water.luban.demo.order.main.service.impl;


import cn.water.demo.luban.protocol.cart.param.CartDeleteParam;
import cn.water.demo.luban.protocol.order.dos.OrderDO;
import cn.water.demo.luban.protocol.order.param.OrderAddParam;
import cn.water.demo.luban.protocol.stock.param.StockReduceCountParam;
import cn.water.luban.demo.cart.api.CartApi;
import cn.water.luban.demo.order.main.mapper.OrderMapper;
import cn.water.luban.demo.order.main.service.OrderService;
import cn.water.luban.demo.stock.api.StockApi;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private StockApi stockApi;
    @Autowired(required = false)
    private CartApi cartApi;
    @Override public void addOrder(OrderAddParam param) {
        String orderSn = param.getOrderSn();
        Integer count = orderMapper.countByOrderSn(orderSn);
        if(count>0){
            return;
        }
        // 减库存
        StockReduceCountParam stockReduceCountParam=new StockReduceCountParam();
        stockReduceCountParam.setReduceCount(param.getCount());
        stockReduceCountParam.setProductCode(param.getProductCode());
        stockReduceCountParam.setOrderSn(param.getOrderSn());
        stockApi.StockReduce(stockReduceCountParam);
//        stockService.reduceCommodityCount(stockReduceCountParam);
        //增订单
        OrderDO orderDO=new OrderDO();
        BeanUtils.copyProperties(param,orderDO);
        orderMapper.insertOrder(orderDO);

        Entry entry=null;
        //删除购物车
        try{
            entry= SphU.entry("cartDelete");
            CartDeleteParam cartDeleteParam=new CartDeleteParam();
            cartDeleteParam.setUserId(param.getUserId());
            cartDeleteParam.setProductCode(param.getProductCode());
            //异步调用无法拿到返回值，必须通过dubbo异步处理，才能拿到返回值
            Boolean cartDelete = cartApi.cartDelete(cartDeleteParam);
            //cartService.deleteCart(cartDeleteParam);
            log.info("获取到cartDelete返回值:"+cartDelete);
            log.info("调用删除购物车后续代码");
            RpcContext rpcContext = RpcContext.getContext();
            Future<Object> future = rpcContext.getFuture();
            //拿到反馈的结果
            Object o = future.get();
            Boolean result = (Boolean) o;
            System.out.println("通过异步调用拿到的result值:"+result);
        }catch (BlockException e){
            //处理sentinel 熔断降级逻辑
            //详细记录日志 谁 什么时间 哪张订单 删除哪个购物车 失败
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            if (entry!=null)entry.exit();
        }

    }
}
